package com.tfswx.fljdmxyy.validation;

import cn.hutool.core.bean.BeanUtil;
import lombok.NonNull;
import org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorContext;
import org.hibernate.validator.internal.engine.path.PathImpl;

import javax.validation.ConstraintValidatorContext;
import java.util.Map;

/**
 * @author ricky
 * @date 2022/5/26 19:19
 */
public class ValidUtil {
    private ValidUtil() {
    }

    static String getPath(ConstraintValidatorContext context) {
        PathImpl basePath = BeanUtil.getProperty(context, "basePath");
        return basePath.toString();
    }

    static HibernateConstraintValidatorContext setDefaultParam(ConstraintValidatorContext context) {
        HibernateConstraintValidatorContext hibernateContext = context.unwrap(HibernateConstraintValidatorContext.class);
        hibernateContext.addMessageParameter("path", getPath(context));
        return hibernateContext;
    }

    static void setParam(ConstraintValidatorContext context, @NonNull Map<String, ?> map) {
        HibernateConstraintValidatorContext hibernateContext = setDefaultParam(context);
        for (Map.Entry<String, ?> entry : map.entrySet()) {
            hibernateContext.addMessageParameter(entry.getKey(), entry.getValue());
        }
    }
}
